<?php
/**
 *  @author Pham Van Diep
 *  @date: 18/2/2014
 *  @description: save update club
 */
// not login
// not perm ad
if(!isset($AppUI->id) || 
   !isset($_SESSION['club_img']) ||   
   (isset($_SESSION['club_img']) && ($_SESSION['club_img']&1<<1) == 0 )
) {
    redirect(HOST_BACKEND);
}
// data input
// error
$arrErr = array();
//image source type: 0-> club, 1-> player .... see languge common.php file options -> controller
$intSrcType  = isset($_POST['srcType'])?intval($_POST['srcType']):0;
$intClubId   = isset($_POST['clubId'])?intval($_POST['clubId']):0;
$strName     = $this->input->post('name',null);
$strShort    = $this->input->post('sshort',null);
$strDes      = $this->input->post('description',null);
$strTag      = $this->input->post('tag',null);
// strip tag
$strName         = CI_Function::stripTag($strName   , 'name');
$strShort        = CI_Function::stripTag($strShort  , 'short');
$strDes          = CI_Function::stripTag($strDes    , 'fckadmin');
$strTag          = CI_Function::stripTag($strTag    , 'tag');
// length
$intLenName      = strlen($strName);
$intLenShort     = strlen($strShort);
$intLenDesc      = strlen($strDes); 
if(empty($strName)) {
    array_push($arrErr,$arrLocale['name_required']);
}elseif($intLen < VL_NAME_MINLENGTH || $intLen > VL_NAME_MAXLENGTH ) {
	array_push($arrErr,$arrLocale['name_range']);
}	
if(!empty($strShort) && ($intLenShort <= VL_SHORT_MINLENGTH || $intLenShort > VL_SHORT_MAXLENGTH )) {
	array_push($arrErr,$arrLocale['short_condition']);
} 	
if($intLenDesc <= VL_CONTENT_MINLENGTH ) {
	array_push($arrErr,$arrLocale['description_minlength']);
}
if(!isset($arrErr)){
    $this->load->library('upload');
    $arrFile = $this->upload->do_upload('file');
    if (count($arrFile) == 0) {
        array_push($arrErr,$arrLocale['file_upload_error']);
    }
}

// save
if(isset($arrErr) && count($arrErr) == 0) {
    if(isset($_SESSION['IMAGE_ERROR']) ) unset($_SESSION['IMAGE_ERROR']);
    if(isset($_SESSION['IMAGE_POST'])) unset($_SESSION['IMAGE_POST']);
    if ($intSrcType == 0) { $intSrcId = 0 ; }else{ $intSrcId = $intSrcType;}
    $strShort = !empty($strShort) ? $strShort : CI_Function::wordCut($strDes);
    $ret = $this->image->add($AppUI->id ,
                             $intClubId,
                             $intSrcId,
                             $intSrcType, 
                             $strName, 
                             $strTag, 
                             $strShort,
                             $strDes, 
                             trim($arrFile['path']), 
                             trim($arrFile['firstname']), 
                             trim($arrFile['extension']), 
                             intval($arrFile['width']), 
                             intval($arrFile['height']) 
                        );    
    if($ret == 0) CI_Function::setMsg('');
    else array_push($arrErr,$arrLocale['file_upload_error']);
}
// occur error
if (isset($arrErr) && count($arrErr) > 0) {
   $_SESSION['IMAGE_POST']  = $this->input->post();
   $_SESSION['IMAGE_ERROR'] = $arrErr;
   redirect(HOST_BACKEND . 'image/add?src=' . $intSrcType.'&id='.$intClubId );
}
redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
